package com.usian.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.common.constant.user.UserConstants;
import com.usian.model.article.pojos.ApAuthor;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.media.pojos.WmUser;
import com.usian.model.user.dtos.AuthDto;
import com.usian.model.user.pojos.ApUser;
import com.usian.model.user.pojos.ApUserRealname;
import com.usian.user.feign.ArticleFeign;
import com.usian.user.feign.WemediaFeign;
import com.usian.user.mapper.ApUserMapper;
import com.usian.user.mapper.AuthUserMpper;
import com.usian.user.service.AuthUserService;


import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * ClassName: ApUserRealnameServiceImpl
 * Author:   WGG
 * Date:    2022-11-2022/11/14-10:35
 * Version: 1.0
 * Description:
 */
@Service
public class AuthUserServiceImpl extends ServiceImpl<AuthUserMpper, ApUserRealname> implements AuthUserService {

    @Resource
    private ApUserMapper apUserMapper;

    @Resource
    WemediaFeign wemediaFeign;
    @Resource
    ArticleFeign articleFeign;


    @Override
    public ResponseResult list(AuthDto dto) {
        //判断参数是否为空
        if (dto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //校验分页参数，初始化
        dto.checkParam();
        //设置查询条件
        LambdaQueryWrapper<ApUserRealname> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(dto.getStatus()!=null, ApUserRealname::getStatus,dto.getStatus());
        //创建分页对象
        Page pageInfo = new Page(dto.getPage(),dto.getSize());
        //执行分页查询
        IPage page = page(pageInfo, queryWrapper);
        //响应数据
        ResponseResult responseResult = new PageResponseResult(dto.getPage(),dto.getSize(),(int)page.getTotal());
        responseResult.setData(page.getRecords());
        return responseResult;
    }

    /**
     * 通过 驳回 状态修改
     * @param dto
     * @param status
     * @return
     */
//    @GlobalTransactional
    @Override
    public ResponseResult updateStatusById(AuthDto dto, Integer status) {
       //检查参数
        if (dto==null || dto.getId() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        if (statusCheck(status)){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2、修改ap_name_user表中的状态
        ApUserRealname apUserRealname = new ApUserRealname();
        apUserRealname.setId(dto.getId());
        apUserRealname.setStatus(status);
        if (dto.getMsg() != null){
            apUserRealname.setReason(dto.getMsg());
        }
        updateById(apUserRealname);
        //3、添加自媒体人账号,添加作者信息
        if (status.equals(UserConstants.PASS_AUTH)){
            ResponseResult createResult = createWmUserAndAuthor(dto);
            if (createResult != null){
                return createResult;
            }
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 创建自媒体账号，以及作者账号
     * @param dto
     * @return
     */
    private ResponseResult createWmUserAndAuthor(AuthDto dto){
        //添加自媒体账号，查询ap_user信息封装到wmuser中
        ApUserRealname aur = getById(dto.getId());
        ApUser apUser = apUserMapper.selectById(aur.getUserId());
        if (apUser == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //检测自媒体用户是否存在
        WmUser wmUser = wemediaFeign.findByName(apUser.getName());
        if (wmUser == null || wmUser.getId() == null){
            wmUser = new WmUser();
            //设置ApUserId
            wmUser.setApUserId(apUser.getId());
            wmUser.setCreatedTime(new Date());
            wmUser.setSalt(apUser.getSalt());
            wmUser.setName(apUser.getPassword());
            wmUser.setPassword(apUser.getPassword());
            wmUser.setStatus(9);
            wmUser.setPhone(apUser.getPhone());
            wemediaFeign.save(wmUser);
        }
        //创建作者账号
        createAuthor(wmUser);
        //修改ap_user标记
        apUser.setFlag(1);
        apUserMapper.updateById(apUser);
        return null;
    }

    /**
     * 创建自媒体账号
     * @param wmUser
     */
    private void createAuthor(WmUser wmUser){
        Integer apUserId = wmUser.getApUserId();
        ApAuthor apAuthor = articleFeign.findByUserId(apUserId);
        if (apAuthor == null){
            apAuthor = new ApAuthor();
            apAuthor.setName(wmUser.getName());
            apAuthor.setType(UserConstants.AUTHOR_TYPE);
            apAuthor.setCreatedTime(new Date());
            apAuthor.setUserId(apUserId);
            articleFeign.save(apAuthor);

        }
    }
    /**
     * 状态监测
     * @param status
     * @return
     */
    public boolean statusCheck(Integer status){
        if (status==null || (status.equals(UserConstants.FAIL_AUTH)
                &&status.equals(UserConstants.PASS_AUTH))){
            return true;
        }
        return false;
//        if(status != null){
//            return false;
//        }
//        return true;
    }
}
